package com.example.crm.servlet;

import com.example.crm.pojo.Admin;
import com.example.crm.service.Impl.AdminServiceImpl;
import com.example.crm.service.IAdminService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginchk")
public class LoginCheckServlet extends HttpServlet {
    IAdminService adminService = new AdminServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("从ServletContext中获取spring容器");

        // 从ServletContext中获取spring容器
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

        IAdminService adminService = (IAdminService)applicationContext.getBean("adminService");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Admin admin = adminService.login(username,password);

        if(admin==null)
        {
            resp.sendRedirect("login.jsp");
        }
        else
        {
            resp.sendRedirect("index.jsp");
        }

    }
}
